import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            LoginFrame loginFrame = new LoginFrame();
            loginFrame.setVisible(true);
        });
    }

    // 登录界面
    static class LoginFrame extends JFrame {
        private JTextField usernameField;
        private JPasswordField passwordField;

        public LoginFrame() {
            setTitle("图书管理系统 - 登录");
            setSize(400, 300);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);

            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(5, 5, 5, 5);

            JLabel titleLabel = new JLabel("图书管理系统");
            titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.gridwidth = 2;
            panel.add(titleLabel, gbc);

            JLabel usernameLabel = new JLabel("用户名:");
            gbc.gridx = 0;
            gbc.gridy = 1;
            gbc.gridwidth = 1;
            panel.add(usernameLabel, gbc);

            usernameField = new JTextField(15);
            gbc.gridx = 1;
            gbc.gridy = 1;
            panel.add(usernameField, gbc);

            JLabel passwordLabel = new JLabel("密码:");
            gbc.gridx = 0;
            gbc.gridy = 2;
            panel.add(passwordLabel, gbc);

            passwordField = new JPasswordField(15);
            gbc.gridx = 1;
            gbc.gridy = 2;
            panel.add(passwordField, gbc);

            JButton loginButton = new JButton("登录");
            gbc.gridx = 0;
            gbc.gridy = 3;
            gbc.gridwidth = 2;
            panel.add(loginButton, gbc);

            loginButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String username = usernameField.getText();
                    String password = new String(passwordField.getPassword());

                    // 简单的登录验证，实际应用中应连接数据库验证
                    if ("admin".equals(username) && "admin123".equals(password)) {
                        JOptionPane.showMessageDialog(LoginFrame.this, "登录成功!");
                        dispose();
                        new MainFrame().setVisible(true);
                    } else {
                        JOptionPane.showMessageDialog(LoginFrame.this, "用户名或密码错误!", "错误", JOptionPane.ERROR_MESSAGE);
                    }
                }
            });

            add(panel);
        }
    }

    // 主界面
    static class MainFrame extends JFrame {
        private JTabbedPane tabbedPane;

        public MainFrame() {
            setTitle("图书管理系统");
            setSize(800, 600);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);

            // 创建菜单栏
            JMenuBar menuBar = new JMenuBar();

            JMenu systemMenu = new JMenu("系统");
            JMenuItem exitItem = new JMenuItem("退出");
            exitItem.addActionListener(e -> System.exit(0));
            systemMenu.add(exitItem);

            JMenu helpMenu = new JMenu("帮助");
            JMenuItem aboutItem = new JMenuItem("关于");
            aboutItem.addActionListener(e -> JOptionPane.showMessageDialog(MainFrame.this, "图书管理系统 v1.0"));
            helpMenu.add(aboutItem);

            menuBar.add(systemMenu);
            menuBar.add(helpMenu);
            setJMenuBar(menuBar);

            // 创建标签页
            tabbedPane = new JTabbedPane();
            tabbedPane.addTab("图书管理", new BookPanel());
            tabbedPane.addTab("读者管理", new ReaderPanel());
            tabbedPane.addTab("借阅管理", new BorrowPanel());
            tabbedPane.addTab("借阅统计", new StatisticsPanel());

            add(tabbedPane);
        }
    }
}